﻿using BUS;
using DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBV_Nhom8
{
    public partial class frmPhieuKhamBenh : Form
    {
        public frmPhieuKhamBenh()
        {
            InitializeComponent();
        }

        private void btnKhamDichVu_Click(object sender, EventArgs e)
        {
            frmYeuCauKhamDichVu obj = new frmYeuCauKhamDichVu();
            obj.ShowDialog();
            dataGridView2.DataSource = ChiTietPhieuXetNghiemBUS.LayDanhSachChiTietPhieuXetNghiemTheoMaXN(Common.MaPXN);
        }

        private void btnHenTaiKham_Click(object sender, EventArgs e)
        {
            frmHenTaiKham obj = new frmHenTaiKham();
            obj.ShowDialog();
            dataGridView2.DataSource = ChiTietPhieuXetNghiemBUS.LayDanhSachChiTietPhieuXetNghiemTheoMaXN(Common.MaPXN);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        DataTable dt = new DataTable();
        private void frmPhieuKhamBenh_Load(object sender, EventArgs e)
        {
            txtTenBacSi.Text = Common.SessionNV.TenNhanVien;
            txtTenBacSi.Enabled = false;

            dt = PhieuKhamBenhBUS.PhieuKhamBenhTrongNgayDaThanhToan();

            cboPhieuKhamBenh.DataSource = dt;
            cboPhieuKhamBenh.DisplayMember = "MaPhieu";
            cboPhieuKhamBenh.ValueMember = "MaPKB";




            dataGridView1.AutoGenerateColumns = false;
            List<User> users = new List<User>();
            users.Add(new User() { Name = "Giảm đau", Id = 1 });
            users.Add(new User() { Name = "Nhức đầu", Id = 2 });
            users.Add(new User() { Name = "Chóng mặt", Id = 3 });
            DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
            DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();
            col.DataSource = users;
            col.DisplayMember = "Name";
            col.DataPropertyName = "UserId";
            col.ValueMember = "Id";
            col.AutoComplete = true;

            dataGridView1.Columns.Add(col);
            dataGridView1.Columns.Add(col1);
            dataGridView1.Columns.Add(col2);
            dataGridView1.Columns.Add(col3);


            dataGridView1.Columns[0].HeaderText = "Tên Thuốc";
            dataGridView1.Columns[1].HeaderText = "Số Lượng";
            dataGridView1.Columns[2].HeaderText = "Công Dụng";
            dataGridView1.Columns[3].HeaderText = "Cách Dùng";

        }

        private void cboPhieuKhamBenh_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (cboPhieuKhamBenh.Text == dt.Rows[i][1].ToString())
                    {
                        PhieuKhamBenh obj = new PhieuKhamBenh();
                        obj.MaPKB = dt.Rows[i][0].ToString();
                        obj.MaPhieu = dt.Rows[i][1].ToString();
                        obj.TenBenhNhan = dt.Rows[i][2].ToString();
                        Common.PhieuKhamBenh = obj;
                        txtHoTen.Text = dt.Rows[i][2].ToString();
                        break;
                    }
                }
            }
            catch
            {
            }
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            ToaThuocDTO toathuoc = new ToaThuocDTO();
            toathuoc.TongTien = 0;
            toathuoc.MaPKB = Convert.ToInt32(Common.PhieuKhamBenh.MaPKB);
            int matt = ToaThuocBUS.ThemToaThuoc(toathuoc);

            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                ChiTietToaThuocDTO obj = new ChiTietToaThuocDTO();
                obj.MaTT = matt;
                obj.MaThuoc = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString());
                obj.SoLuong = Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value.ToString());
                obj.CongDung = dataGridView1.Rows[i].Cells[2].Value.ToString();
                obj.CachDung = dataGridView1.Rows[i].Cells[3].Value.ToString();
                ChiTietToaThuocBUS.ThemChiTietToaThuoc(obj);
            }
            MessageBox.Show("Lập Phiếu Khám Bệnh Thành Công");
            this.Close();
        }
    }

    public class User
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }

    public class Account
    {
        public string AccountName { get; set; }
        public int UserId { get; set; }
    }
}
